package com.yunji.storage.provide;

import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.storage.bo.ExistsStockBO;
import com.yunji.storage.bo.StorageRecordsListQueryBO;
import com.yunji.storage.bo.StorageTicketSelectQueryBO;
import com.yunji.storage.business.StorageInteractBusiness;
import com.yunji.storage.dao.IStockDao;
import com.yunji.storage.dao.IStorageRecordsDao;
import com.yunji.storage.dao.IStorageTicketDao;
import com.yunji.storage.domain.StockDO;
import com.yunji.storage.domain.StorageRecordsDO;
import com.yunji.storage.domain.StorageTicketDO;
import com.yunji.storage.dto.StorageOperateDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Project: yunji-coffee
 * @Package: com.yunji.storage.provide
 * @Title: StockProvide
 * <p>
 * History:
 * Date                     Version     Author          Summary
 * ============================================================
 * 2024-10-24 19:44:47      V1.0        HuaAo       新建类
 */
@Component
@Slf4j
public class StockProvide {

    @Resource
    IStockDao stockDao;

    @Resource
    IStorageTicketDao storageTicketDao;

    @Resource
    IStorageRecordsDao storageRecordsDao;

    @Resource
    StorageInteractBusiness interactBusiness;

    public void handleStock(List<StockDO> stockList) {
        // 同个商品无法做处理只能先处理数据库

        for (StockDO stock : stockList) {
            StockDO s = stockDao.selectStock(stock);

            if (BeanUtils.isNotNull(s)) {
                stock.setAllStock(stock.getAllStock().add(s.getAllStock()));
                stock.setUsableStock(stock.getUsableStock().add(s.getUsableStock()));
                stock.setFrozenStock(stock.getFrozenStock().add(s.getFrozenStock()));
                stock.setDisableStock(stock.getDisableStock().add(s.getDisableStock()));
                stock.setStockId(s.getStockId());
                Assert.isTrue(stockDao.edit(stock), "修改失败");
            } else {
                Assert.isTrue(stockDao.create(stock), "新增失败");
            }

        }
    }

    public boolean updateStockTicketStatus(StorageTicketDO ticket) {
        return storageTicketDao.updateStatus(ticket);
    }

    public boolean existsStock(ExistsStockBO bo) {
        return stockDao.existsStock(bo);
    }

    public boolean createStorageTicket(StorageTicketDO ticketDO) {
        return storageTicketDao.save(ticketDO);
    }

    public boolean createStorageRecord(List<StorageRecordsDO> recordsList) {
        return storageRecordsDao.saveBatch(recordsList);
    }

    public StorageTicketDO selectStorageTicket(StorageTicketSelectQueryBO bo) {
        return storageTicketDao.select(bo);
    }

    public List<StorageRecordsDO> getStorageRecords(StorageRecordsListQueryBO param) {
        return storageRecordsDao.search(param);
    }

    public void operateStorage(StorageOperateDTO operateDTO) {
        interactBusiness.operateStorage(operateDTO);
    }
}